package vn.gq.tym.truyennguoilonfull;

import vn.gq.tym.utils.NetworkUtils;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class NoNetworkActivity extends BaseActivity {

	private ProgressBar mProgressBar;
	private Intent mIntent;
	private Handler mHandler = new Handler();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.no_network_layout);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		mProgressBar = (ProgressBar) findViewById(R.id.loading);
		mProgressBar.setVisibility(View.GONE);
		mIntent = (Intent) getIntent().getExtras().get("INTENT");
		final TextView label = (TextView) findViewById(R.id.label_no_network);
		((Button) findViewById(R.id.btn_reconnect)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(final View btnRetry) {
				mProgressBar.setVisibility(View.VISIBLE);
				label.setVisibility(View.GONE);
				btnRetry.setVisibility(View.GONE);
				if (NetworkUtils.newInstance(NoNetworkActivity.this).hasConnection()) {
					mHandler.postDelayed(new Runnable() {
						
						@Override
						public void run() {
							mProgressBar.setVisibility(View.GONE);
						}
					}, 500);
					startActivity(mIntent);
					finish();
				} else {
					mHandler.postDelayed(new Runnable() {
						
						@Override
						public void run() {
							mProgressBar.setVisibility(View.GONE);
							label.setVisibility(View.VISIBLE);
							btnRetry.setVisibility(View.VISIBLE);
						}
					}, 1000);
				}
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.none, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
			case android.R.id.home:
				onBackPressed();
				break;
		}
		return super.onOptionsItemSelected(item);
	}

}
